//
//  RZQCuriousChooseViewController.m
//  LastOne
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQCuriousChooseViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "ChooseModel.h"
#import "RZQCuriousChooseCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "RZQReplayViewController.h"
#import "NightHeader.h"
@interface RZQCuriousChooseViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)UIImageView *image;
@property(nonatomic, strong)ChooseModel *ticket;
@property(nonatomic, assign)NSInteger Flag;




@end

@implementation RZQCuriousChooseViewController
-(void)viewWillAppear:(BOOL)animated
{
  
}
#pragma mark 网络请求
-(void)getNet
{
    _dataSource = [NSMutableArray array];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://app.qdaily.com/app/papers/detail/%ld.json", _modelData.iD] BodyDic:nil WithDataBlock:^(id data) {
       
        NSDictionary *dic = [data objectForKey:@"response"];
        NSArray *array = [dic objectForKey:@"questions"];
        NSDictionary *Dic = array[0];
        NSArray *Array = [Dic objectForKey:@"options"];
        for (NSDictionary *dic  in Array) {
            ChooseModel *model = [[ChooseModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:model];
        }
        [_collectionView reloadData];
     
        
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getNet];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(350 * FitWidth, 80 * FitHeight);
    flowLayout.minimumLineSpacing = 5 * FitWidth;
    flowLayout.sectionInset = UIEdgeInsetsMake(5 * FitHeight, 5 * FitWidth, 5 * FitHeight, 5 * FitWidth);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113 * FitHeight) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentInset = UIEdgeInsetsMake(200 * FitHeight, 0, 0, 0);
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(80 * FitWidth , ScreenHeight - 108 * FitHeight, 200 * FitWidth, 40 * FitHeight);
    button.layer.borderWidth = 2.0;
    button.layer.cornerRadius = 20;
    [button setTitle:@"投票" forState:0];

    [button addTarget:self action:@selector(ticketAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _Flag = -1;
    
    [_collectionView registerClass:[RZQCuriousChooseCollectionViewCell class] forCellWithReuseIdentifier:@"choose"];
    
    [_collectionView NightWithType:UIViewColorTypeNormal];
    
    
    
    _image = [[UIImageView alloc]initWithFrame:CGRectMake(0, -200 * FitHeight, ScreenWidth, 200 * FitHeight)];
    [_image sd_setImageWithURL:[NSURL URLWithString:_modelData.image]];
    _image.backgroundColor = [UIColor redColor];
    _image.contentMode = UIViewContentModeScaleAspectFill;
    
    [_collectionView addSubview:_image];
    
    
    // Do any additional setup after loading the view.
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y -  0 *FitHeight;
    
    if (y <= -200 * FitHeight) {
        CGRect  frame = _image.frame;
        frame.origin.y = y;
        frame.size.height = -y;
        _image.frame = frame;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    RZQCuriousChooseCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"choose" forIndexPath:indexPath];
    cell.modelData = _dataSource[indexPath.row];
    //cell.backgroundColor = [UIColor grayColor];
    cell.choose.backgroundColor = [UIColor grayColor];
    cell.choose.layer.borderWidth = 2.0;
    cell.choose.layer.cornerRadius = 10;
    if (indexPath.row == _Flag) {
        cell.choose.backgroundColor = [UIColor redColor];
    }

    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.hidesBottomBarWhenPushed = YES;
    return _dataSource.count;
   
}
-(void)ticketAction
{
    RZQReplayViewController *re = [[RZQReplayViewController alloc]init];
    [self.navigationController pushViewController:re animated:YES];
    re.cirData = _modelData;
    
    re.netData = _ticket;
    
    
   // http://app.qdaily.com/app/papers/
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    _Flag = indexPath.row;
    _ticket =  _dataSource[indexPath.row];
    [_collectionView reloadData];
  
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
